#!/usr/bin/env python
# -*- coding: utf-8 -*- 

#Kiwi Bot
#Make by @nge1nCorsic4 in 2010
#Modified by @nge1nCorsic4 in 2011


#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License along
#with this program; if not, write to the Free Software Foundation, Inc.,
#51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import kiwi, kiwi.plugin, kiwi.events, kiwi.config, kiwi.lang

class welcome(kiwi.plugin.Plugin):
	def onStart(self):
		self.clientList = {}
		self.bdWelcome = kiwi.bd.bd_welcome()
		self.lang = kiwi.lang.Lang(self, 'welcome')
		ServNumber = kiwi.config.configuration().kiwi.kiwidir
		self.conf = kiwi.config.configuration('@kiwi/conf/'+ServNumber+'/welcome.cfg')
		self.friends =  int(self.conf.settings.friends)
		self.members = int(self.conf.settings.members)
		self.compteur = kiwi.getPath(self.conf.settings.comptage)
		self.registerEvent(kiwi.events.EVT_CLIENT_CONNECT, self.client_connect)
		self.registerEvent(kiwi.events.EVT_CLIENT_DISCONNECT, self.client_deco)
		self.registerEvent(kiwi.events.EVT_CLIENT_UPDATE, self.welcome)
	def onStop(self):
		pass
	def client_connect(self, event):
		if not self.clientList.has_key(event.id):
			self.clientList[event.id] = 1
	def client_deco(self, event):
		if self.clientList.has_key(event.id):
			del self.clientList[event.id]
	def welcome(self, event):
		if self.clientList.has_key(event.id):
			if self.clientList[event.id] == 1 :
				if int(event.vars['t']) != 3 :
					c = self.clients.getClientById(event.id)[0]
					if c.rights == self.friends:
						self.output.say(self.lang.lang(event,"00202")%(c.name))
					elif c.rights >= self.members :
						self.output.say(self.lang.lang(event,"00200")%(c.name))
					else :
						self.output.tell(event.id, self.lang.lang(event,"00203")%(c.name))
					self.clientList[event.id] = 2
					if self.compteur == 'on':
						self.Wiew_Connect(c)
	def Wiew_Connect(self, c):
		id,guid = c.id,c.guid
		count = self.bdWelcome.checkClient(guid)
		# print count
		if count :
			count += 1
			self.bdWelcome.updateId(guid,count)
			self.output.tell(c.id,self.lang.lang(c,"00205")%count)
		else:
			self.output.tell(c.id,self.lang.lang(c,"00206"))
			self.bdWelcome.insertId(guid)
